/*
 * @Author: haungyan
 * @Date: 2020-11-24 23:35:22
 * @LastEditors: haungyan
 * @LastEditTime: 2020-11-24 23:45:28
 * @Description: file content
 * @FilePath: \mini-vue\js\watcher.js
 */
class Watcher {
    constructor(vm, key, cb) {
        this.vm = vm
        this.key = key
        this.cb = cb // 回调更新视图

        Dep.target = this // 把watcher对象记录到Dep类的静态属性target
        this.oldValue = vm[key]
        Dep.target = null
    }
    update() {
        let newValue = this.vm[this.key]
        if (this.oldValue === newValue) {
            return
        }
        this.cb(newValue)
    }
}